package com.nuoniu.sibanyun.service.erp.barcode.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.dto.BarcodeMainInfoDto;
import com.nuoniu.sibanyun.entity.dto.BarcodeMainInfoSearchDto;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeMainInfo;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodePrintDetail;
import com.nuoniu.sibanyun.mapper.erp.barcode.ErpBarcodeMainInfoMapper;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeMainInfoService;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodePrintDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 条码管理-条码主档 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-11-10
 */
@Service
@Log4j2
public class ErpBarcodeMainInfoServiceImpl extends ServiceImpl<ErpBarcodeMainInfoMapper, ErpBarcodeMainInfo> implements IErpBarcodeMainInfoService {


    @Autowired
    private IErpBarcodePrintDetailService barcodePrintDetailService;

    @Override
    public List<ErpBarcodeMainInfo> analysisByBarCode(BarcodeMainInfoSearchDto barcodeMainInfoSearchDto) {
        UserInfo userInfo = UserThreadLocal.get();
        String barcode = barcodeMainInfoSearchDto.getBarcode();
        Long ruleId = barcodeMainInfoSearchDto.getRuleId();
        log.info("条码解析：租户名称：{}.用户明细：{},条码：{}，规则：{}",userInfo.getCompanyName(),userInfo.getRealName(),barcode,ruleId);
        List<String> barcodeList = Arrays.asList(barcode.split(","));
        //查询 主档
        QueryWrapper<ErpBarcodeMainInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("bar_code",barcodeList);
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        if( null != ruleId){
            queryWrapper.eq("barcode_rule_id",ruleId);
        }
        List<ErpBarcodeMainInfo> erpBarcodeMainInfoList = list(queryWrapper);
        List<String> findBarcodes = erpBarcodeMainInfoList.stream().map(ErpBarcodeMainInfo::getBarCode).collect(Collectors.toList());
        List<String> notFindBarcodes = barcodeList.stream().filter(barcodeStr -> !findBarcodes.contains(barcodeStr)).collect(Collectors.toList());
        // 如果没有在主档找到，然后去打印明细查找
        if(null != notFindBarcodes){
            BarcodeMainInfoSearchDto searchDto = new BarcodeMainInfoSearchDto();
            searchDto.setRuleId(barcodeMainInfoSearchDto.getRuleId());
            for (int i = 0; i < notFindBarcodes.size(); i++) {
                String barCode = notFindBarcodes.get(i);
                searchDto.setBarcode(barCode);
                ErpBarcodePrintDetail detail = barcodePrintDetailService.getByCondition(searchDto);
                if(null != detail && StringUtils.isNotBlank(detail.getExtendJson())){
                    ErpBarcodeMainInfo barcodeMainInfo = new ErpBarcodeMainInfo();
                    barcodeMainInfo.setBarCode(barCode);
                    barcodeMainInfo.setExtendJson(detail.getExtendJson());
                    erpBarcodeMainInfoList.add(barcodeMainInfo);
                }
            }
        }
        return erpBarcodeMainInfoList;
    }

    @Override
    public void checkSourceVoucherType(Integer sourceVoucherId, String sourceVoucherType) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpBarcodeMainInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("source_voucher_id",sourceVoucherId);
        queryWrapper.eq("source_voucher_type",sourceVoucherType);
        int count = count(queryWrapper);
        if(count > 0) {
            throw new NuoNiuException("当前单据明细的条码已经生成");
        }
    }

    @Override
    public List<ErpBarcodeMainInfo> listByDetailIdsAndSourceVoucherType(BarcodeMainInfoDto barcodeMainInfoDto) {
        List<Integer> detailIds = barcodeMainInfoDto.getDetailIds();
        String sourceVoucherType = barcodeMainInfoDto.getSourceVoucherType();
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpBarcodeMainInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.in("source_detail_id",detailIds);
        queryWrapper.eq("source_voucher_type",sourceVoucherType);
        queryWrapper.select("id","company_id","source_detail_id","source_voucher_type","bar_code","extend_json","cum_print_times");
        List<ErpBarcodeMainInfo> barcodeMainInfoList = list(queryWrapper);
        return barcodeMainInfoList;
    }
}
